import os
import pandas as pd
import chardet
import shutil
from tqdm import tqdm  # 用于显示进度条
'''

用于将文件夹2中的数据合并至文件夹1
'''

def detect_encoding(file_path):
    """检测文件编码"""
    with open(file_path, 'rb') as f:
        result = chardet.detect(f.read())
    return result['encoding'] if result['confidence'] > 0.7 else 'gbk'


# 配置路径 (按需修改)
folder1_path = r"D:\AAAAA\zhanjiang\2022-2023"  # 原始文件夹1
folder2_path = r"D:\AAAAA\zhanjiang\Temperature\2022-2023"  # 原始文件夹2
merged_folder_path = r"D:\AAAAA\test\2022-2023"  # 新文件夹路径


# 确保新文件夹存在
os.makedirs(merged_folder_path, exist_ok=True)

# 获取文件夹1中的所有子文件夹名
subfolders = [name for name in os.listdir(folder1_path)
              if os.path.isdir(os.path.join(folder1_path, name))]

print(f"找到 {len(subfolders)} 个子文件夹")
print("开始处理文件...")

# 遍历每个子文件夹
for subfolder in tqdm(subfolders, desc="处理子文件夹"):
    folder1_sub = os.path.join(folder1_path, subfolder)
    folder2_sub = os.path.join(folder2_path, subfolder)
    merged_sub = os.path.join(merged_folder_path, subfolder)

    # 确保目标子文件夹存在
    os.makedirs(merged_sub, exist_ok=True)

    # 获取子文件夹中的CSV文件
    csv_files = [f for f in os.listdir(folder1_sub) if f.endswith('.csv')]

    # 处理每个CSV文件
    for csv_file in csv_files:
        file1_path = os.path.join(folder1_sub, csv_file)
        file2_path = os.path.join(folder2_sub, csv_file)
        merged_path = os.path.join(merged_sub, csv_file)

        # 检查文件是否存在
        if not os.path.exists(file2_path):
            print(f"\n警告: {file2_path} 不存在，已复制原始文件")
            shutil.copy(file1_path, merged_path)
            continue

        try:
            # 检测编码并读取文件
            enc1 = detect_encoding(file1_path)
            enc2 = detect_encoding(file2_path)

            df1 = pd.read_csv(file1_path, encoding=enc1)
            df2 = pd.read_csv(file2_path, encoding=enc2)

            # 合并数据 (横向拼接)
            merged_df = pd.concat([df1, df2], axis=1)

            # 保存合并后的CSV (使用UTF-8 with BOM确保兼容性)
            merged_df.to_csv(merged_path, index=False, encoding='utf_8_sig')

        except Exception as e:
            print(f"\n处理文件 {csv_file} 时出错: {str(e)}")
            print(f"尝试使用备用方案...")

            # 备用方案：直接拼接文本文件
            try:
                with open(file1_path, 'r', encoding=enc1) as f1, \
                        open(file2_path, 'r', encoding=enc2) as f2, \
                        open(merged_path, 'w', encoding='utf_8_sig') as out_f:

                    # 读取所有行
                    lines1 = f1.readlines()
                    lines2 = f2.readlines()

                    # 确保行数相同
                    if len(lines1) != len(lines2):
                        raise ValueError(f"文件行数不同: {len(lines1)} vs {len(lines2)}")

                    # 合并每行内容
                    for i in range(len(lines1)):
                        line1 = lines1[i].strip()
                        line2 = lines2[i].strip()

                        # 处理标题行（第一行）
                        if i == 0:
                            # 移除可能存在的BOM字符
                            if line1.startswith('\ufeff'):
                                line1 = line1[1:]
                            if line2.startswith('\ufeff'):
                                line2 = line2[1:]

                            # 合并标题
                            combined = f"{line1},{line2}"
                        else:
                            # 合并数据行
                            combined = f"{line1},{line2.split(',', 1)[1]}" if ',' in line2 else f"{line1},{line2}"

                        out_f.write(combined + '\n')

                print(f"备用方案成功: {csv_file}")

            except Exception as e2:
                print(f"备用方案失败: {str(e2)}")
                print(f"复制原始文件: {file1_path}")
                shutil.copy(file1_path, merged_path)

print("=" * 50)
print(f"操作完成! 合并后的文件已保存至: {merged_folder_path}")
print(f"成功处理 {len(subfolders)} 个子文件夹")